/**
 * 20182018年1月16日
 *
 * @author zhuwh4
 */
package com.utils.failureanalysis.data.util;

import com.utils.failureanalysis.data.model.FilePath;
import com.utils.failureanalysis.exceptions.fileNotFoundException.PathIsNullException;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zhuwh4
 *
 */
public class FindFilePath {

    // 路径公用部分
    // D:\jenkins\workspace\Attila-4-Lite-功能-RW-2018-1-15-18-48-14-339_Contacts\android-cts
    private List<String> headPath = new ArrayList<>();

    private List<FilePath> filePath = new ArrayList<>();

    private String testId;

    private String taskId;

    public FindFilePath(File path, String testId) throws PathIsNullException {
        this.testId = testId;
        System.out.println(headPath);
        findPathHead(path);
    }

    public String getTaskId(){
        return taskId;
    }
    /**
     * 扫描获取所有匹配测试任务ID的路径根
     *
     * @param path 用户输入的测试任务ID
     */
    private void findPathHead(File path) throws PathIsNullException {
//		弹出console 请求用户输入测试任务ID
//		Console console = System.console();
//		if (console == null) {
//			throw new IllegalStateException("Console is not available!");
//		}
//		String subPath = console.readLine("Please input test ID:");

        if (path.list() == null) {
            throw new PathIsNullException();
        }

        for (String s : path.list()) {
            System.out.println(s);
            System.out.println(".*-" + testId + "_.*");
            System.out.println(s.matches(".*-" + testId + "_.*"));
            if (s.matches(".*-" + testId + "_.*")) {
//                Attila-4-Lite-功能-RW-2018-1-15-18-48-14-339_Contacts
                taskId=s.replace("_.*","").replaceAll("[^\\d-]","").replaceAll(".*--+","").replaceAll(testId+".*",testId);
                System.out.println("find taskId is "+taskId);
                testId=taskId;
//				String module = s.split("_")[1];
                System.out.println(path + "\\" + s + "\\android-cts\\results");
                headPath.add(path + "\\" + s + "\\android-cts");
            }
        }
    }

    public List<FilePath> getFilePath() {
        for (String head : headPath) {
//				获取所有路径，构建FilePath对象
            File failResultFilePath = findFailResultFilePath(head);
            File resultFilePath = findResultFilePath(head);
            filePath.add(new FilePath(failResultFilePath, resultFilePath, new File(head)));
        }
        return filePath;
    }

    /**
     * 获取 test_result_failures.html路径
     *
     * @param head
     * @return
     */
    private File findFailResultFilePath(String head) {
        return new File(head + "\\results\\test_result_failures.html");
    }


    /**
     * 获取test_result.xml路径
     *
     * @param head
     * @return
     */
    private File findResultFilePath(String head) {
        return new File(head + "\\results\\test_result.xml");
    }


}
